package ltd.followkba.page;

import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.kcbsdk.EventHandler;

import ltd.advskin.MRAM;
import ltd.advskin.MSKIN;
import ltd.advskin.base.BasePage;
import ltd.advskin.base.KcTypeListener;
import ltd.advskin.utils.SubUtils;
import ltd.advskin.view.KcHeaderView;
import ltd.followkba.MAPP;
import ltd.followkba.R;
import ltd.followkba.W;
import ltd.followkba.task.BDbsAccount;
import ltd.followkba.task.LogTyp;
import ltd.followkba.task.RunUtils;
import main.MAPI;

import static ltd.advskin.MSKIN.MSTRING;
import static ltd.advskin.MSKIN.MTOAST;
import static main.MAPI.mMyWorks;

public class AutoCarePage extends BasePage {
    private EditText etCare0;
    private EditText etCare1;
    private EditText etCare2;
    private EditText etCare3;
    private TextView tvCare9;

    // 所有的页面都接受同样的方式打开LOG
    public static boolean logEnable;

    public void MLOG(String text) {
        if (logEnable) {
            MSTRING(MAPI.GET_LOG_NAME(this) + text);
        }
    }

    @Override
    public void onInitView() {                       // 使用者需要继承，初始化页面控件等显示
        setLayout(R.layout.page_auto_care);
        mKcHeaderView = (KcHeaderView) findViewById(R.id.khvCare);
        mKcHeaderView.setTitle("设置自动关注", new KcTypeListener() {
            @Override
            public void onMessage(int type) {
                switch (type) {
                    case KcHeaderView.TYPE_CLICK_LEFT:
                        onFinish();
                        break;

                }
            }
        });
        etCare0 = findViewById(R.id.etCare0);
        etCare1 = findViewById(R.id.etCare1);
        etCare2 = findViewById(R.id.etCare2);
        etCare3 = findViewById(R.id.etCare3);
        tvCare9 = findViewById(R.id.tvCare9);
        findViewById(R.id.kbrSaveCare).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 保存上面的条件
                if (MAPP.mBDbsAccount.CareSet == null){
                    MAPP.mBDbsAccount.CareSet = new int[5];
                    for (int index = 0; index < MAPP.mBDbsAccount.CareSet.length; index++){
                        MAPP.mBDbsAccount.CareSet[index] = -1;
                    }
                }
                for (int index = 0; index < 4; index++) {
                    saveCareSet(index);
                }
                boolean negative = true;
                for (int index = 0; index < MAPP.mBDbsAccount.CareSet.length; index++){
                    if (MAPP.mBDbsAccount.CareSet[index] != -1){
                        negative = false;
                        break;
                    }
                }
                if (negative){              // 全部都是-1
                    MAPP.mBDbsAccount.CareSet = null;
                }
                MTOAST("已保存设置", 2000);
                RunUtils.saveBDbsAccount(MAPP.mBDbsAccount);
            }
        });

        findViewById(R.id.kbtCare9).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 第9行保存
                MAPP.startCommentTextPage(LogTyp.CARE_TEXT);
            }
        });


    }
    @Override
    public void onInitData(){                                                                       // onInitView结束，开始加载数据
        showInfo();
    }
    @Override
    public void onFinish() {       // 使用者选择性继承，用户按了后退键，不继承就直接关闭了
        SubUtils subUtils = new SubUtils();
        subUtils.hideInputMethod();
        setClose();
    }

    private void showInfo(){
        if (MAPP.mBDbsAccount.CareSet != null && MAPP.mBDbsAccount.CareSet.length >= 4){
            MSKIN.setText(String.format("%s", MAPP.mBDbsAccount.CareSet[0]), etCare0);
            MSKIN.setText(String.format("%s", MAPP.mBDbsAccount.CareSet[1]), etCare1);
            MSKIN.setText(String.format("%s", MAPP.mBDbsAccount.CareSet[2]), etCare2);
            MSKIN.setText(String.format("%s", MAPP.mBDbsAccount.CareSet[3]), etCare3);

        }else {
            MSKIN.setText("-1", etCare0);
            MSKIN.setText("-1", etCare1);
            MSKIN.setText("-1", etCare2);
            MSKIN.setText("-1", etCare3);
        }
        MSKIN.setText(MAPP.mBDbsAccount.TextComment[W.TEXT_CARE], tvCare9);
//        MSKIN.setText(MAPP.textsToText(MAPP.mBDbsAccount.CareText), tvCare9);
    }
    private void saveCareSet(int index){
        EditText[] editTexts = {etCare0, etCare1, etCare2, etCare3};
        String text = editTexts[index].getText().toString();
        int value = "-1".equals(text) ? -1 : MAPI.STRING_DEC32(text);
        MAPP.mBDbsAccount.CareSet[index] = value;
    }

}
